<?php
/**
 * Created by PhpStorm.
 * User: jack Wang
 * Date: 2021/12/8
 * Time: 16:20
 */

class Getdata extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Activity_model');
    }
    /*
     *  文章的爬取脚本
     * /usr/bin/php /var/www/html/iblog/index.php job/Getdata/getArticle
     */
    public function getArticle()
    {
        $url = "https://blog.csdn.net/";
        $rules = [
            // 采集文章标题
            'title' => ['.title > h2', 'text'],
            //文章内容
            'desc' => ['.summary', 'text'],
            //文章详情链接
            'link' => ['.title > h2 a', 'href'],
        ];
        $range = "#feedlist_id > li";
        $query = getQuery();
        $data = $query->query($url,$rules,$range);
        foreach ($data as $v){
            $res = $this->Activity_model->commonInsert('article',$v);
            $this->getContent($v['link']);
        }
    }

    public function getContent($url)
    {
        $rules = [
            // 采集文章标题
            'content' => ['', 'html'],
        ];
        $range = "#content_views";
        $query = getQuery();
        $data = $query->query($url,$rules,$range);
        return $data[0]['content'];
//        $res = $this->Activity_model->commonUpdate('article',$data[0],['link'=>$url]);
    }

    //获取指定Csdn用户的文章
    public function getUserArticle()
    {
       $url = "https://blog.csdn.net/qq_40298175?type=blog";
        $rules = [
            // 采集文章标题
            'title' => ['.blog-list-box-top > h4', 'text'],
            //文章内容
            'desc' => ['.blog-list-content', 'text'],
            //文章详情链接
            'link' => ['a', 'href'],
        ];
        $range = ".mainContent .blog-list-box";
        $query = getQuery();
        $data = $query->query($url,$rules,$range);
        foreach ($data as $v){
            $body[] = $this->getContent($v['link']);
        }
//        var_dump($body);die;
    }

    public function getImage()
    {
        $url = "https://s.weibo.com/pic?q=%E5%9B%BE%E7%89%87&Refer=weibo_pic";
        var_dump(curl_request($url));die;
        $rules = [
            // 采集文章标题
            'img' => ['.img img','src'],
        ];
        $range = "#pl_pic_feedlist .pic";
        $query = getQuery();
        $data = $query->query($url,$rules,$range);
        var_dump($data);
    }
}